﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.Serialization;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using TrackerRealm.Jazz.Common;
using TrackerRealm.Jazz.Client;
using TrackerRealm.Jazz.Client.Services;

namespace TrackerRealm.Jazz.Json
{
    /// <summary>
    /// 
    /// </summary>
    public class jStateConverter : JsonConverter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="value"></param>
        /// <param name="serializer"></param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            jState state = (jState)value;
            if (state.IsEmpty)
            {
                writer.WriteValue(state.Name);
            }
            else
            {
                writer.WriteStartObject();
                writer.WritePropertyName("name");
                writer.WriteValue(state.Name);
                writer.WritePropertyName("class");
                writer.WriteValue(state.MethodInfo.DeclaringType.FullName);
                writer.WriteEndObject();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="objectType"></param>
        /// <param name="existingValue"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.String)
            {
                return jState.Empty;
            }
            reader.Read();
            string p_name = (string)reader.Value;
            reader.Read();
            string stateName = (string)reader.Value;
            reader.Read();
            string p_className = (string)reader.Value;
            reader.Read();
            string className = (string)reader.Value;
            Type t = JazzServices.GetTypeFromName(className);
            StatesCollection states = StatesCollection.Create(t);
            jState state = states.FirstOrDefault(s => s.Name == stateName);
            reader.Read();
            return state;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectType"></param>
        /// <returns></returns>
        public override bool CanConvert(Type objectType)
        {
            if (objectType == typeof(jState))
                return true;
            return false;
        }
    }
}
